package com.zhiche.lisa.lspm.service.sys.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.enums.TableStatusEnum;
import com.zhiche.lisa.lspm.dao.mapper.sys.PermissionMapper;
import com.zhiche.lisa.lspm.dao.model.sys.Permission;
import com.zhiche.lisa.lspm.dao.model.sys.RolePermission;
import com.zhiche.lisa.lspm.service.sys.IPermissionService;
import com.zhiche.lisa.lspm.service.sys.IRolePermissionService;
import com.zhiche.lisa.lspm.service.sys.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限 service impl
 *
 * @author hxh
 * @since 2018-09-28
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    IRolePermissionService rolePermissionService;

    @Autowired
    IRoleService roleService;
    @Override
    public List<Permission> queryAllUsablePermission() {
        EntityWrapper<Permission> ew = new EntityWrapper<>();
        ew.eq("status", TableStatusEnum.STATUS_1.getCode())
                .orderBy("id", true);
        return selectList(ew);
    }

    @Override
    public Permission[] levelOne(Integer roleId) {
        Wrapper<RolePermission> rolePermissionWrapper=new EntityWrapper<>();
        rolePermissionWrapper.eq("role_id",roleId);
        List<RolePermission> rolePermissions = rolePermissionService.selectList(rolePermissionWrapper);
        List<Integer> permissionId=new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            permissionId.add(rolePermission.getPermissionId());
        }
        Wrapper<Permission> permissionWrapper=new EntityWrapper<>();
        permissionWrapper.in("id",permissionId);
        List<Permission> permissions = this.selectList(permissionWrapper);
        List<Permission> levelOne=new ArrayList<>();
        for (Permission permission : permissions) {
            if (1==permission.getType())
                levelOne.add(permission);
        }
        return  levelOne.toArray(new Permission[]{});
    }

    @Override
    public Permission[] levelTwo(Integer roleId,Integer pid) {
        List<Permission> levelOne=new ArrayList<>();

        Wrapper<RolePermission> rolePermissionWrapper=new EntityWrapper<>();
        rolePermissionWrapper.eq("role_id",roleId);
        List<RolePermission> rolePermissions = rolePermissionService.selectList(rolePermissionWrapper);
        List<Integer> permissionId=new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            permissionId.add(rolePermission.getPermissionId());
        }
        Wrapper<Permission> allPermissionWrapper=new EntityWrapper<>();
        allPermissionWrapper.in("id",permissionId);
        List<Permission> allPermissions = this.selectList(allPermissionWrapper);
        if (pid==0){
            for (Permission permission : allPermissions) {
                if (1!=permission.getType()) {
                    levelOne.add(permission);
                }
            }
            return levelOne.toArray(new Permission[]{});
        }
        Wrapper<Permission> permissionWrapper=new EntityWrapper<>();
        permissionWrapper.in("id",permissionId).eq("pid",pid);
        List<Permission> permissions = this.selectList(permissionWrapper);
        for (Permission permission : permissions) {
            if (1!=permission.getType()) {
                levelOne.add(permission);
                Wrapper<Permission> permissionEntityWrapper=new EntityWrapper<>();
                permissionEntityWrapper.eq("pid",permission.getOrders());
                List<Permission> permissionList = this.selectList(permissionEntityWrapper);
                if (!CollectionUtils.isEmpty(permissionList)){
                    for (Permission permission1 : permissionList) {
                        for (Permission allPermission : allPermissions){
                            if (allPermission.getId().equals(permission1.getId()))
                                levelOne.add(permission1);
                        }
                    }
                }
            }
        }
        return  levelOne.toArray(new Permission[]{});
    }
}
